home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 343_02 / edge2.c < prev    next >
Text File  |  1991-04-18  |  10KB  |  374 lines

  1.  
  2.        /***********************************************
  3.        *
  4.        *       file d:\cips\edge2.c
  5.        *
  6.        *       Functions: This file contains
  7.        *          homogeneity
  8.        *          difference_edge
  9.        *          contrast_edge
  10.        *
  11.        *       Purpose:
  12.        *          These functions implement several
  13.        *          types of advanced edge detection.
  14.        *
  15.        *       External Calls:
  16.        *          wtiff.c - does_not_exist
  17.        *                    round_off_image_size
  18.        *                    create_allocate_tiff_file
  19.        *                    write_array_into_tiff_image
  20.        *          tiff.c - read_tiff_header
  21.        *          rtiff.c - read_tiff_image
  22.        *          numcvrt.c - get_integer
  23.        *          edge.c - fix_edges
  24.        *
  25.        *       Modifications:
  26.        *          26 March 1991 - created
  27.        *
  28.        *************************************************/
  29.  
  30. #include "d:\cips\cips.h"
  31.  
  32.  
  33.  
  34. short e_mask[3][3] = {
  35.        {-9,  0, -9},
  36.        { 0, 36,  0},
  37.        {-9,  0, -9} };
  38.  
  39. short contrast[3][3] = {
  40.    {  1,  1,  1},
  41.    {  1,  1,  1},
  42.    {  1,  1,  1}};
  43.  
  44.  
  45.    /**************************************************
  46.    *
  47.    *   homogeneity(...
  48.    *
  49.    *   This function performs edge detection by looking
  50.    *   for the absence of an edge.  The center of a
  51.    *   3x3 area is replaced by the absolute value of
  52.    *   the max difference between the center point
  53.    *   and its 8 neighbors.
  54.    *
  55.    ***************************************************/
  56.  
  57.  
  58. homogeneity(in_name, out_name, the_image, out_image,
  59.              il, ie, ll, le, threshold, high)
  60.    char   in_name[], out_name[];
  61.    int    high, il, ie, ll, le, threshold;
  62.    short  the_image[ROWS][COLS], out_image[ROWS][COLS];
  63. {
  64.    int a, b, absdiff, absmax, diff, i, j,
  65.        length, max, max_diff, new_hi, new_low, width;
  66.  
  67.    struct tiff_header_struct image_header;
  68.  
  69.    if(does_not_exist(out_name)){
  70.       printf("\n\nHOMO> output file does not exist %s",
  71.              out_name);
  72.       read_tiff_header(in_name, &image_header);
  73.       round_off_image_size(&image_header,
  74.                            &length, &width);
  75.       image_header.image_length = length*ROWS;
  76.       image_header.image_width  = width*COLS;
  77.       create_allocate_tiff_file(out_name, &image_header,
  78.                                 out_image);
  79.    }  /* ends if does_not_exist */
  80.  
  81.    read_tiff_header(in_name, &image_header);
  82.  
  83.    new_hi  = 250;
  84.    new_low = 16;
  85.    if(image_header.bits_per_pixel == 4){
  86.        new_hi  = 10;
  87.        new_low = 3;
  88.    }
  89.  
  90.    max = 255;
  91.    if(image_header.bits_per_pixel == 4)
  92.       max = 16;
  93.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  94.  
  95.    for(i=0; i<ROWS; i++)
  96.       if( (i%10) == 0) printf("%3d", i);
  97.       for(j=0; j<COLS; j++)
  98.          out_image[i][j] = 0;
  99.  
  100.    for(i=1; i<ROWS-1; i++){
  101.       for(j=1; j<COLS-1; j++){
  102.  
  103.           max_diff = 0;
  104.           for(a=-1; a<=1; a++){
  105.              for(b=-1; b<=1; b++){
  106.  
  107.                 diff = the_image[i][j] - the_image[i+a][j+b];
  108.                 absdiff = abs(diff);
  109.                 if(absdiff > max_diff) max_diff = absdiff;
  110.  
  111.              }  /* ends loop over b */
  112.           }  /* ends loop over a */
  113.  
  114.           out_image[i][j] = max_diff;
  115.       }  /* ends loop over j */
  116.    }  /* ends loop over i */
  117.  
  118.  
  119.      /* if desired, threshold the output image */
  120.    if(threshold == 1){
  121.        for(i=0; i<ROWS; i++){
  122.           for(j=0; j<COLS; j++){
  123.              if(out_image[i][j] > high){
  124.                   out_image[i][j] = new_hi;
  125.              }
  126.              else{
  127.                   out_image[i][j] = new_low;
  128.              }
  129.           }
  130.        }
  131.    }  /* ends if threshold == 1 */
  132.  
  133.    fix_edges(out_image, 1);
  134.  
  135.    write_array_into_tiff_image(out_name, out_image,
  136.                                il, ie, ll, le);
  137.  
  138.  
  139. } /* ends homogeneity */
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.    /**************************************************
  149.    *
  150.    *   difference_edge(...
  151.    *
  152.    *   This function performs edge detection by looking
  153.    *   at the differences in the pixels that surround
  154.    *   the center point of a 3x3 area.  It replaces the
  155.    *   center point with the absolute value of the
  156.    *   max difference of:
  157.    *      upper left - lower right
  158.    *      upper right - lower left
  159.    *      left - right
  160.    *      top - bottom
  161.    *
  162.    ***************************************************/
  163.  
  164. difference_edge(in_name, out_name, the_image, out_image,
  165.                 il, ie, ll, le, threshold, high)
  166.    char   in_name[], out_name[];
  167.    int    high, il, ie, ll, le, threshold;
  168.    short  the_image[ROWS][COLS], out_image[ROWS][COLS];
  169. {
  170.    int a, b, absdiff, absmax, diff, i, j,
  171.        length, max, max_diff, new_hi, new_low, width;
  172.  
  173.    struct tiff_header_struct image_header;
  174.  
  175.  
  176.    if(does_not_exist(out_name)){
  177.       printf("\n\nDIFF> output file does not exist %s",
  178.              out_name);
  179.       read_tiff_header(in_name, &image_header);
  180.       round_off_image_size(&image_header,
  181.                            &length, &width);
  182.       image_header.image_length = length*ROWS;
  183.       image_header.image_width  = width*COLS;
  184.       create_allocate_tiff_file(out_name, &image_header,
  185.                                 out_image);
  186.    }  /* ends if does_not_exist */
  187.  
  188.    read_tiff_header(in_name, &image_header);
  189.  
  190.    new_hi  = 250;
  191.    new_low = 16;
  192.    if(image_header.bits_per_pixel == 4){
  193.        new_hi  = 10;
  194.        new_low = 3;
  195.    }
  196.  
  197.    max = 255;
  198.    if(image_header.bits_per_pixel == 4)
  199.       max = 16;
  200.  
  201.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  202.  
  203.    for(i=0; i<ROWS; i++)
  204.       for(j=0; j<COLS; j++)
  205.          out_image[i][j] = 0;
  206.  
  207.    for(i=1; i<ROWS-1; i++){
  208.       if( (i%10) == 0) printf("%3d", i);
  209.       for(j=1; j<COLS-1; j++){
  210.  
  211.           max_diff = 0;
  212.           absdiff = abs(the_image[i-1][j-1] -
  213.                          the_image[i+1][j+1]);
  214.           if(absdiff > max_diff) max_diff = absdiff;
  215.  
  216.           absdiff = abs(the_image[i-1][j+1] -
  217.                          the_image[i+1][j-1]);
  218.           if(absdiff > max_diff) max_diff = absdiff;
  219.  
  220.           absdiff = abs(the_image[i][j-1] -
  221.                          the_image[i][j+1]);
  222.           if(absdiff > max_diff) max_diff = absdiff;
  223.  
  224.           absdiff = abs(the_image[i-1][j] -
  225.                          the_image[i+1][j]);
  226.           if(absdiff > max_diff) max_diff = absdiff;
  227.  
  228.  
  229.           out_image[i][j] = max_diff;
  230.  
  231.       }  /* ends loop over j */
  232.    }  /* ends loop over i */
  233.  
  234.  
  235.  
  236.      /* if desired, threshold the output image */
  237.    if(threshold == 1){
  238.        for(i=0; i<ROWS; i++){
  239.           for(j=0; j<COLS; j++){
  240.              if(out_image[i][j] > high){
  241.                   out_image[i][j] = new_hi;
  242.              }
  243.              else{
  244.                   out_image[i][j] = new_low;
  245.              }
  246.           }
  247.        }
  248.    }  /* ends if threshold == 1 */
  249.  
  250.  
  251.  
  252.    fix_edges(out_image, 1);
  253.    write_array_into_tiff_image(out_name, out_image,
  254.                                il, ie, ll, le);
  255.  
  256.  
  257. } /* ends difference_edge */
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.    /**************************************************
  269.    *
  270.    *   contrast_edge(...
  271.    *
  272.    *   The edge detector uses the basic quick edge
  273.    *   detector mask and then divides the result
  274.    *   by a contrast smooth mask.  This implements
  275.    *   Johnson's contrast based edge detector.
  276.    *
  277.    ***************************************************/
  278.  
  279. contrast_edge(in_name, out_name, the_image, out_image,
  280.                 il, ie, ll, le, threshold, high)
  281.    char   in_name[], out_name[];
  282.    int    high, il, ie, ll, le, threshold;
  283.    short  the_image[ROWS][COLS], out_image[ROWS][COLS];
  284. {
  285.    int ad, d;
  286.    int a, b, absdiff, absmax, diff, i, j,
  287.        length, max, new_hi, new_low, sum_d, sum_n, width;
  288.  
  289.    struct tiff_header_struct image_header;
  290.  
  291.  
  292.    if(does_not_exist(out_name)){
  293.       printf("\n\nCONTRAST> output file does not exist %s",
  294.              out_name);
  295.       read_tiff_header(in_name, &image_header);
  296.       round_off_image_size(&image_header,
  297.